<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      padding: 0;
      margin: 0;
    }
    .scene {
      width: 100%;
      height: 100vh;
      text-align: center;
    }
    .sensor-data {
      display: flex;
      align-items: center;
    }
    .sensor-data .sensor-item {
      padding: 10px 0;
      width: 50%;
      font-size: 24px;
      background: rgba(0, 0, 0, .6);
      color: #fff;
    }
    .house-block {
      width: 100%;
      height: 70%;
      position: fixed;
      bottom: 0;
    }
    .house {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
      width: 100%;
    }
    .js-rain {
      display: none;
    }
    .rain {
      margin-top: 40px;
      width: 200px;
      animation: rainrun infinite ease-in-out 5s;
    }
    .rainshed {
      position: absolute;
      left: 0;
      right: 0;
      margin: auto;
      bottom: 30%;
      z-index: 2;
    }
    @keyframes rainrun {
      0% {
        transform: translateX(0);
      }
      20% {
        transform: translateX(50%);
      }
      80% {
        transform: translateX(0);
      }
      100% {
        transform: translateX(-50%);
      }
    }
  </style>
</head>

<body>  
  <div class="scene">
    <div class="sensor-data">
      <div class="sensor-item">温度: <span id="js-tem"></span></div>
      <div class="sensor-item">湿度: <span id="js-hum"></span></div>
    </div>
    <div class="house-block">
      <img class="rain js-rain" src="./imgs/rain.png" />
      <img class="rainshed js-rain" src="./imgs/rainshed.png" alt="">
      <img class="house" src="./imgs/house.jpg" alt="">
    </div>
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
  <script>
    try {
      if (!localStorage.getItem('userToken')) {
        location.href = '/login.html'
      }
    } catch (error) {
      location.href = '/login.html'
    }
    getSensorData()
    getRainStatus()
    setInterval(() => {
      getSensorData()
      getRainStatus()
    }, 5000)
    function getSensorData() {
      axios.get('/sensor').then(res => {
        document.getElementById('js-tem').innerText = res.data.tem + '℃'
        document.getElementById('js-hum').innerText = res.data.hum + '%'
      })
    }
    function getRainStatus() {
      axios.get('/rainshed').then(res => {
        const status = res.data.status
        console.log(status)
        if (status) {
          document.querySelectorAll('.js-rain').forEach(e => {
            e.style.display = 'unset'
          })
        } else {
          document.querySelectorAll('.js-rain').forEach(e => {
            e.style.display = 'none'
          })
        }
      })
    }
  </script>
</body>

</html>